<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars2d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,二维,地图,地球,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 二维 地图 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 二维地图 全景漫游 地理信息系统 云GIS 二维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="//mars2d.cn/favicon.ico" />
    <title>Tooltip 鼠标移入信息窗 | Mars2D | 地理信息平台 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars2d"
    ></script>

    <link href="css/divGraphic.css" rel="stylesheet" />
    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars2dContainer" class="mars2d-container"></div>

    <!-- 面板 -->
    <div class="infoview">
      <div>
        <button id="btnGetCenter" class="btn btn-primary" onclick="bindMapDemo()">Map上直接弹出</button>
        <button id="btnGetCenter" class="btn btn-primary" onclick="bindLayerDemo()">图层上绑定</button>
        <button id="btnGetCenter" class="btn btn-primary" onclick="bindLayerDemo2()">图层上预定义配置</button>
      </div>
      <div>
        <button id="btnGetCenter" class="btn btn-primary" onclick="bindGraphicDemo1()">Graphic上绑定</button>
        <button id="btnGetCenter" class="btn btn-primary" onclick="bindGraphicDemo2()">Graphic上局部刷新</button>
      </div>

      <!--
      <span>其他应用示例：</span>
      <a href="editor.html#f40_cylinder_gdp" target="_blank" class="btn btn-default">自定义模版</a> -->
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;
      var graphicLayer;
      var geoJsonLayer;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars2d.Util.merge(options, {
          zoom: 12, center: { lng: 117.309471, lat: 31.797018 },
        });

        //创建地图
        map = new mars2d.Map("mars2dContainer", mapOptions);

        //创建矢量数据图层
        graphicLayer = new mars2d.layer.GraphicLayer();
        map.addLayer(graphicLayer);

        // map.on(mars2d.EventType.tooltipopen, function (event) {
        //   let container = event.tooltip._container; //tooltip对应的DOM
        //   console.log("打开了tooltip(全局监听)", event);
        // });
        // map.on(mars2d.EventType.tooltipclose, function (event) {
        //   let container = event.tooltip._container; //tooltip对应的DOM
        //   console.log("关闭了tooltip(全局监听)", event);
        // });

        bindLayerDemo();
      }

      function removeDemoLayer() {
        //关闭弹窗
        if(lastMaplTooltip){
          map.closeTooltip(lastMaplTooltip);
          delete lastMaplTooltip
        }

        graphicLayer.clear();

        if (geoJsonLayer) {
          geoJsonLayer.remove(true);
          geoJsonLayer = null;
        }
      }

      let lastMaplTooltip

      //1.在map地图上绑定Tooltip单击弹窗
      function bindMapDemo() {
        removeDemoLayer();

        if(lastMaplTooltip){
          map.closeTooltip(lastMaplTooltip);
          delete lastMaplTooltip
        }

        //传入坐标和内容，可以直接任意弹出,当前获取的是地图的中心点
        let position = map.getCenter();
        lastMaplTooltip = new L.Tooltip().setContent("我是地图上直接弹出的").setLatLng(position);
        map.openTooltip(lastMaplTooltip);
      }

      //2.在layer图层上绑定Tooltip单击弹窗
      function bindLayerDemo() {
        removeDemoLayer();

        geoJsonLayer = new mars2d.layer.GeoJsonLayer({
          name: "标绘示例数据",
          url: "//data.mars2d.cn/file/geojson/mars2d-draw.json",
        });
        map.addLayer(geoJsonLayer);

        //在layer上绑定Tooltip单击弹窗
        geoJsonLayer.bindTooltip(function (event) {
          let attr = event.attr;
          return attr.type + " 我是layer上绑定的Tooltip";

          // return new Promise((resolve) => {
          //   //这里可以进行后端接口请求数据，setTimeout测试异步
          //   setTimeout(() => {
          //     resolve('Promise异步回调显示的弹窗内容信息')
          //   }, 2000)
          // })
        });

        // geoJsonLayer.on(mars2d.EventType.tooltipopen, function (event) {
        //   let container = event.tooltip._container; //tooltip对应的DOM
        //   console.log("图层上打开了tooltip", container);
        // });
        // geoJsonLayer.on(mars2d.EventType.tooltipclose, function (event) {
        //   let container = event.tooltip._container; //tooltip对应的DOM
        //   console.log("图层上移除了tooltip", container);
        // });
      }

      //2.在layer图层上预定义tooltip单击弹窗
      function bindLayerDemo2() {
        removeDemoLayer();

        geoJsonLayer = new mars2d.layer.GeoJsonLayer({
          name: "标绘示例数据",
          url: "//data.mars2d.cn/file/geojson/mars2d-draw.json",
          //tooltip按属性字段配置，可以是字符串模板或数组
          // tooltip: 'all', //显示所有属性，常用于测试
          // tooltip: '{name} {type}',
          tooltip: [
            { field: "id", name: "编码" },
            { field: "name", name: "名称" },
            { field: "type", name: "类型" },
          ],
          tooltipOptions: { direction: "right", offsetX: 10 },
        });
        map.addLayer(geoJsonLayer);
      }

      //3.在graphic数据上绑定Tooltip单击弹窗
      function bindGraphicDemo1() {
        removeDemoLayer();

        let graphic = new mars2d.graphic.Marker({
          latlng: [31.854628, 117.245425],
          style: {
            image: "img/marker/mark1.png",
            width: 32,
            height: 44,
          },
        });
        graphicLayer.addGraphic(graphic);

        function getInnerHtml(event) {
          // let attr = event.graphic.attr
          var inthtml = `<table style="width:280px;">
                <tr><th scope="col" colspan="4"  style="text-align:center;font-size:15px;">graphic.bindTooltip</th></tr>
                <tr><td >说明：</td><td >Tooltip鼠标单击信息弹窗 </td></tr>
                <tr><td >方式：</td><td >可以绑定任意html </td></tr>
                <tr><td >备注：</td><td >我是graphic上绑定的Tooltip</td></tr>
              </table>`;
          return inthtml;
        }

        //绑定Tooltip
        graphic.bindTooltip(getInnerHtml, { direction: "right" }).openTooltip();

        // graphic.on(mars2d.EventType.tooltipopen, function (event) {
        //   let container = event.tooltip._container; //tooltip对应的DOM
        //   console.log("打开了tooltip", container);
        // });
        // graphic.on(mars2d.EventType.tooltipclose, function (event) {
        //   let container = event.tooltip._container; //tooltip对应的DOM
        //   console.log("移除了tooltip", container);
        // });
      }

      //3.在graphic数据上绑定Tooltip单击弹窗
      function bindGraphicDemo2() {
        removeDemoLayer();

        let graphic = new mars2d.graphic.Marker({
          latlng: [31.854628, 117.245425],
          style: {
            image: "img/marker/mark1.png",
            width: 32,
            height: 44,
          },
        });
        graphicLayer.addGraphic(graphic);

        let timeTik;

        //刷新局部DOM,不影响tooltip面板的其他控件操作
        graphic.on(mars2d.EventType.tooltipopen, function (event) {
          let container = event.tooltip._container; //tooltip对应的DOM
          console.log("打开了tooltip", container);

          let tdTime = container.querySelector("#tdTime");

          clearInterval(timeTik);
          timeTik = setInterval(() => {
            if (tdTime) {
              var date = new Date().format("yyyy-MM-dd HH:mm:ss S");
              tdTime.innerHTML = date;
            }
          }, 500);
        });

        graphic.on(mars2d.EventType.tooltipclose, function (event) {
          let container = event.tooltip._container; //tooltip对应的DOM
          console.log("移除了tooltip", container);

          clearInterval(timeTik);
        });

        let innerHtml = `<table style="width:280px;">
                <tr><th scope="col" colspan="4"  style="text-align:center;font-size:15px;">graphic.bindTooltip局部刷新</th></tr>
                <tr><td >说明：</td><td >Tooltip鼠标单击信息弹窗 </td></tr>
                <tr><td >方式：</td><td >可以绑定任意html </td></tr>
                <tr><td >备注：</td><td >我是graphic上绑定的Tooltip</td></tr>
                <tr><td >时间：</td><td id="tdTime"></td></tr>
                <tr><td colspan="4" style="text-align:right;cursor: pointer;"><a href="javascript:showXQ()">更多</a></td></tr>
              </table>`;
        //绑定Tooltip
        graphic.bindTooltip(innerHtml, { offsetY: -30 }).openTooltip();
      }

      //只是为了演示，可以单击详情
      function showXQ(id) {
        window.layer.open({
          type: 2,
          title: "查看历史",
          fix: true,
          shadeClose: true,
          maxmin: true,
          area: ["80%", "80%"],
          content: "http://marsgis.cn/",
          skin: "layer-mars-dialog animation-scale-up",
          success: function (layero) {},
        });
      }
    </script>
  </body>
</html>
